define_extension :sources, :copy_update do
  use(:checkout).for(:sources)
  
  set(:scm) { fetch_previous }
  set(:scm_type) { fetch_previous }
  
  task :sources do
    build_repository.setup!
    rev = build_repository.revisions.reverse.find { |r| r < real_revision && build_repository[r].checked_out }
    if rev == nil
      logger.info { "can't find any suitable revision to update. falling back to fresh checkout" }
      execute_previous # will execute checkout due to the 'use' definition in the beginning
    else
      brev = build_repository[real_revision]
      brev.setup!
      FileUtils.rm_rf(brev.src_path)
      FileUtils.cp_r(build_repository[rev].src_path, brev.src_path)
      command = scm.sync(real_revision, brev.src_path)

      logger.debug "executing: #{command}"
      if !syscmd(command)
        raise RuntimeError, "sources update error"
      end
    end 
  end
end
